package com.fastloan.jn.mgt.customermanager.basic.service.impl;

import com.fastloan.jn.common.BasicServiceImpl;
import com.fastloan.jn.mgt.customermanager.basic.dao.JnCustomerManagerMapper;
import com.fastloan.jn.mgt.customermanager.basic.model.JnCustomerManager;
import com.fastloan.jn.mgt.customermanager.basic.model.vo.JnCustomerManagerTree;
import com.fastloan.jn.mgt.customermanager.basic.model.vo.ManagerPerformanceExt;
import com.fastloan.jn.mgt.customermanager.basic.service.JnCustomerManagerService;
import com.fastloan.safe.mgt.user.model.CmuUser;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.kawins.supers.table.manager.DataTable;
import com.kawins.supers.tree.Bean2TreeUtil;
import com.kawins.supers.tree.exp.TreeInitializeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * 济南客户经理Service实现类
 *
 * @author yanwe
 *         createTime 2017-10-2017/10/17 11:16
 */
@Service
public class JnCustomerManagerServiceImpl extends BasicServiceImpl<JnCustomerManager> implements JnCustomerManagerService {

    @Autowired
    private JnCustomerManagerMapper jnCustomerManagerMapper;

    @Autowired
    private CmuUserService cmuUserService;




    @Override
    public JnCustomerManagerTree selectByUserId(String userId){
        return jnCustomerManagerMapper.selectByUserId(userId);
    }

    @Override
    public boolean createCustomerManager(JnCustomerManager jnCustomerManager){
        if (checkNest(jnCustomerManager)) return false;
        int result = insert(jnCustomerManager);
        return result == 1;
    }


    @Override
    public boolean updateCustomerManager(JnCustomerManager jnCustomerManager){
        if (checkNest(jnCustomerManager)) return false;
        int result = updateByPrimaryKeySelective(jnCustomerManager);
        return result == 1;
    }

    @Override
    public List<JnCustomerManagerTree> selectTierTreeByUserId(Integer userId){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        List<JnCustomerManagerTree> customerManagers = selectCusMgrTreeByUserId(map);
        Bean2TreeUtil<JnCustomerManagerTree> treeUtil = new Bean2TreeUtil<>();
        List<JnCustomerManagerTree> result = null;
        try {
            result = treeUtil.getChildNodesById(customerManagers, userId);
        } catch (TreeInitializeException e) {
            e.printStackTrace();
            log.error("获取客户经理错误,userId:"+userId+e.getMessage());
        }
        return result;
    }

    @Override
    public List<JnCustomerManagerTree> selectTierTreeByOrgId(Integer orgId){
        Map<String,Object> map = new HashMap<>();
        map.put("orgId",orgId);
        List<JnCustomerManagerTree> customerManagers = selectCusMgrTreeByOrgId(map);
        Bean2TreeUtil<JnCustomerManagerTree> treeUtil = new Bean2TreeUtil<>();
        return treeUtil.getChildNodesByParentId(customerManagers, 0);
    }

    @Override
    @DataTable(name = "selectCusMgrTreeByUserId")
    public List<JnCustomerManagerTree> selectCusMgrTreeByUserId(Map<String,Object> map){
        return jnCustomerManagerMapper.selectCusMgrTreeByUserId(map);
    }

    @Override
    public List<JnCustomerManagerTree> selectCusMgrTreeByOrgId(Map<String,Object> map){
        return jnCustomerManagerMapper.selectCusMgrTreeByOrgId(map);
    }

    @Override
    public List<String> managerIds(String managerId) {
        return jnCustomerManagerMapper.managerIds(managerId);
    }

    @Override
    @DataTable(name = "queryOrgManagers")
    public List<CmuUser> queryOrgManagers(Map<String,Object> map) {
        return jnCustomerManagerMapper.queryOrgManagers(map);
    }

    @Override
    @DataTable(name = "queryManagerPerformance")
    public List<ManagerPerformanceExt> queryManagerPerformance(Map<String, Object> map) {
        return jnCustomerManagerMapper.queryManagerPerformance(map);
    }

    private boolean checkNest(JnCustomerManager jnCustomerManager) {
        if (jnCustomerManager.getParentId().equals(jnCustomerManager.getUserId())){
            return false;
        }
        String userIds = jnCustomerManagerMapper.selectParentUserIds(jnCustomerManager.getParentId());
        if (userIds != null){
            boolean checkResult = Stream.of(userIds.split(","))
                    .anyMatch(userId -> userId.equals(jnCustomerManager.getUserId()));
            if (checkResult){//父子嵌套
                return true;
            }
        }
        return false;
    }


}
